package com.uusee.crawler.job;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.uusee.crawler.dbwriter.BaikeImageDbWriter;
import com.uusee.crawler.fetcher.FetchHTTP;
import com.uusee.crawler.model.CrawlStatusCodes;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.crawler.pageprocessor.baike.mtime.MoviePostersAndImagesPageProcessor;
import com.uusee.framework.bo.UniversalBo;
import com.uusee.shipshape.bk.model.BaikeImage;

public class MtimeImagesCrawlJob {
	private static final int PAGE_SIZE = 40;
	private Log log = LogFactory.getLog(MtimeImagesCrawlJob.class);
	private FetchHTTP fetch = new FetchHTTP();
	private MoviePostersAndImagesPageProcessor pageProcessor = new MoviePostersAndImagesPageProcessor();
	private BaikeImageDbWriter dbWriter = new BaikeImageDbWriter();
	private String sourceSite = "www.mtime.com";
	//finished:50000-101408
	private int start = 101408;
	private int end =   120000;
	
	public static void main(String[] args) {
		try {
			ApplicationContext acx = new ClassPathXmlApplicationContext("spring/applicationContext-framework.xml");
			MtimeImagesCrawlJob job = new MtimeImagesCrawlJob();
			UniversalBo universalBo = (UniversalBo)acx.getBean("universalBo");
			job.dbWriter.setUniversalBo(universalBo);
			
			job.doExecute();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void doExecute() {
		try {
			StopWatch stopWatch = new StopWatch();
			stopWatch.start();
			
			//抓取
			for (int movieId = start; movieId <=end; movieId++) {
				try {
					int maxPage = getMaxPage(movieId+"");
					if (maxPage>0) {
						log.info(sourceSite+"-抓取海报和图片(电影ID:"+movieId+";图片页数："+maxPage+")");
						for (int page = 1;page<=maxPage;page++) {
							crawl(createCrawlURI(movieId+"",page));
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (movieId%100==0 && movieId!=0) {
					log.info(sourceSite+"-抓取电影数:"+(movieId-start)+";用时:"+stopWatch.getTime()/1000+"秒");
				}
			}
			stopWatch.stop();
			log.info("抓取结束,用时：" + stopWatch.getTime() / 1000 + "秒。");
		} catch (Exception e) {
			log.error(e);
		}
	}
	
	public CrawlURI createCrawlURI(String mtimeMovieId,int page) {
		CrawlURI crawlURI = new CrawlURI();
		String crawlUrl = "http://www.mtime.com/movie/" + mtimeMovieId + "/posters_and_images/index.html";
		if (page>1) {
			crawlUrl = "http://www.mtime.com/movie/" + mtimeMovieId + "/posters_and_images/index-"+page+".html";
		}
		
		crawlURI.setSourceSite("www.mtime.com");
		crawlURI.setCrawlUrl(crawlUrl);
		crawlURI.setCharset("utf-8");
		
		List<BaikeImage> baikeImageList = new ArrayList<BaikeImage>();
		crawlURI.setModel(baikeImageList);
		
		return crawlURI;
	}
	
	public void crawl(String mtimeMovieId) {
		try {
			int maxPage = getMaxPage(mtimeMovieId);
			if (maxPage>0) {
				log.info(sourceSite+"-抓取海报和图片(电影ID:"+mtimeMovieId+";图片页数："+maxPage+")");
				for (int page = 1;page<=maxPage;page++) {
					crawl(createCrawlURI(mtimeMovieId,page));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void crawl(CrawlURI crawlURI) {
		try {
			fetch.process(crawlURI);
			pageProcessor.process(crawlURI);
			if (crawlURI.getCrawlStatus() == CrawlStatusCodes.PAGE_PROCESS_SUCCESS) {
				List<CrawlURI> baikeImageCrawlURIList = (List<CrawlURI>) crawlURI.getModel();
				for (CrawlURI baikeImageCrawlURI:baikeImageCrawlURIList) {
					baikeImageCrawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_SUCCESS);
					dbWriter.process(baikeImageCrawlURI);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public int getMaxPage(String mtimeMovieId) {
		int maxPage = 0;
		try {
			CrawlURI crawlURI = createCrawlURI(mtimeMovieId,1);
			fetch.process(crawlURI);
			int crawlStatus = crawlURI.getCrawlStatus();
			if (crawlStatus == CrawlStatusCodes.FETCH_SUCCESS) {
				String regx = "所有图片<em.*?>.*?共([0-9]*?)张.*?</em>";
				Pattern p = Pattern.compile(regx);
				Matcher m = p.matcher(crawlURI.getCrawlResult());
				if (m.find()) {
					int count = Integer.valueOf(m.group(1));
					if ( count != 0 ) {
						maxPage = count/PAGE_SIZE+1;
					}
				}
			}
		}catch(Exception e){}
		return maxPage;
	}

	public void setFetch(FetchHTTP fetch) {
		this.fetch = fetch;
	}

	public void setPageProcessor(MoviePostersAndImagesPageProcessor pageProcessor) {
		this.pageProcessor = pageProcessor;
	}

	public void setDbWriter(BaikeImageDbWriter dbWriter) {
		this.dbWriter = dbWriter;
	}

	public void setSourceSite(String sourceSite) {
		this.sourceSite = sourceSite;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public void setEnd(int end) {
		this.end = end;
	}
	
	
}
